package com.yu.subject.domain.handler.subject;

import com.yu.subject.common.enums.SubjectInfoTypeEnum;
import com.yu.subject.domain.entity.SubjectAnswerBO;
import com.yu.subject.domain.entity.SubjectInfoBO;
import com.yu.subject.domain.entity.SubjectOptionBO;
import com.yu.subject.infra.basic.entity.SubjectMultiple;
import com.yu.subject.infra.basic.entity.SubjectRadio;
import com.yu.subject.infra.basic.service.SubjectMultipleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 路亚
 * @version 0.1
 * @className RadioTypeHandler
 * @description
 * @date 2024/12/24 22:32
 * @since jdk11
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        // 插入
        List<SubjectAnswerBO> optionList = subjectInfoBO.getOptionList();
        List<SubjectMultiple> list = optionList.stream().map(option -> {
            SubjectMultiple subjectMultiple = new SubjectMultiple();
            BeanUtils.copyProperties(option, subjectMultiple);
            subjectMultiple.setOptionType(Long.valueOf(option.getOptionType()));
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            return subjectMultiple;
        }).collect(Collectors.toList());
        subjectMultipleService.saveBatch(list);
    }

    @Override
    public SubjectOptionBO query(Integer subjectId) {
        return null;
    }
}
